package android.slc.extras.component.attchment.ui.vd;

import android.slc.adapter.CommonlySwipeRecycler;
import android.slc.adapter.click.SlcOnItemChildClickListenerProxy;
import android.slc.appbase.R;
import android.slc.appbase.entity.ErrorMsg;
import android.slc.appbase.ui.utils.ItemDecorationUtils;
import android.slc.appbase.ui.vd.BaseListVd;
import android.slc.attachment.bean.Progress;
import android.slc.extras.component.attchment.domain.AttachmentReviewVmBox;
import android.slc.extras.component.attchment.entity.Attachment;
import android.slc.extras.component.attchment.ui.adapter.AttachmentAdapter;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import com.blankj.utilcode.util.StringUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.android.material.bottomsheet.SlcBottomSheetAlertDialog2;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;

import java.util.List;

/**
 * @Description:
 * @Author: sunlunchang
 * @CreateDate: 2021/8/10 11:26
 */
public abstract class AttachmentBaseVd<VM extends AttachmentReviewVmBox<A>, A extends AttachmentReviewVmBox.AttachmentAdapter> extends BaseListVd<Attachment, VM> {
    private AttachmentAdapter attachmentAdapter;

    @Override
    protected void initMustCondition() {
        super.initMustCondition();
        final Observer<Attachment> notifyAttachmentItemObserver = Attachment -> {
            if (attachmentAdapter != null && Attachment != null) {
                attachmentAdapter.notifyItemChanged(vmBox.getNotifyAttItemDataChangeByIndex());
            }
        };
        this.vmBox.getNotifyAttItemDataChangeLd().observe((AppCompatActivity) activityResultCaller, notifyAttachmentItemObserver);
        /*if (activityResultCaller instanceof AppCompatActivity) {
            this.vmBox.getNotifyAttItemDataChangeLd().observeInActivity((AppCompatActivity) activityResultCaller, notifyAttachmentItemObserver);
        } else if (activityResultCaller instanceof Fragment) {
            this.vmBox.getNotifyAttItemDataChangeLd().observeInFragment((Fragment) activityResultCaller, notifyAttachmentItemObserver);
        }*/
    }

    @Override
    protected void initSwipeRecycler() {
        super.initSwipeRecycler();
        ItemDecorationUtils.addItemDecorationByRecyclerView(swipeRecycler.getRecyclerView());
    }

    @Override
    protected CommonlySwipeRecycler.CommonlySwipeRecyclerBuilder<Attachment> perfectRecyclerBuilder() {
        return super.perfectRecyclerBuilder().setOnLoadAdapterLaterListener(null);
    }

    @Override
    protected BaseQuickAdapter<Attachment, BaseViewHolder> initAdapterAndBaseEvent() {
        BaseQuickAdapter<Attachment, BaseViewHolder> adapter = super.initAdapterAndBaseEvent();
        adapter.addChildClickViewIds(R.id.iv_action);
        adapter.setOnItemChildClickListener(new SlcOnItemChildClickListenerProxy(this::onAdapterItemChildClick));
        return adapter;
    }

    @Override
    protected BaseQuickAdapter<Attachment, BaseViewHolder> getAdapter(List<Attachment> list) {
        return attachmentAdapter = new AttachmentAdapter(list);
    }

    /**
     * item中子空间处理事件
     *
     * @param adapter
     * @param view
     * @param position
     */
    protected void onAdapterItemChildClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (view.getId() == R.id.iv_action) {
            Attachment attachment = (Attachment) adapter.getItem(position);
            Progress progress = attachment.getProgress();
            if (progress == null || progress.status != Progress.ERROR) {
                onRemove(position, attachment);
            } else {
                showHandlerErrorDialog(adapter, attachment, position);
            }
        }
    }

    protected void onRemove(int position, Attachment attachment) {
        vmBox.removeAtt(attachment);
    }

    /**
     * 显示处理错误对话框
     *
     * @param adapter
     * @param attachment
     */
    protected void showHandlerErrorDialog(@NonNull BaseQuickAdapter<?, ?> adapter, Attachment attachment, int position) {
        if (attachment.isLocalBody() && !attachment.isNetBody()) {
            new SlcBottomSheetAlertDialog2.Builder(contentView.getContext())
                    .setItems(R.array.app_attachment_operating_local_array, (dialog, which) -> {
                        dialog.dismiss();
                        switch (which) {
                            case 0://错误信息
                                String message = StringUtils.getString(R.string.label_file_upload_by_file_failed);
                                Progress progress = attachment.getProgress();
                                if (progress != null && progress.extra1 instanceof ErrorMsg) {
                                    ErrorMsg errorMsg = (ErrorMsg) progress.extra1;
                                    message = errorMsg.getMsg();
                                }
                                showErrorMsg(message);
                                break;
                            case 1://重新上传
                                vmBox.getAttachmentAdapter().uploadAttachmentSingleDirectlyUnder(attachment);
                                break;
                            case 2://移除
                                onRemove(position, attachment);
                                break;
                        }
                    }).create().show();
        } else {
            new SlcBottomSheetAlertDialog2.Builder(contentView.getContext())
                    .setItems(R.array.app_attachment_operating_net_array, (dialog, which) -> {
                        dialog.dismiss();
                        switch (which) {
                            case 0://错误信息
                                String message = StringUtils.getString(R.string.label_file_download_failed);
                                Progress progress = attachment.getProgress();
                                if (progress != null && progress.extra1 instanceof ErrorMsg) {
                                    ErrorMsg errorMsg = (ErrorMsg) progress.extra1;
                                    message = errorMsg.getMsg();
                                }
                                showErrorMsg(message);
                                break;
                            case 1://重新下载
                                vmBox.onDownload(attachment);
                                break;
                        }
                    }).create().show();
        }
    }

    /**
     * 显示错误信息
     *
     * @param message
     */
    protected void showErrorMsg(String message) {
        new MaterialAlertDialogBuilder(contentView.getContext())
                .setMessage(message).create().show();
    }
}
